/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i,st,ed) for(int i = st ; i < ed ; ++i )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

class TaxiManager {
public:
    int schedule(vector <string> roads, vector <string> customers);
};

pair<int,int> customer[15];
int D[52][52];
int RC;
int N;
int dp[ (1<<12)+1 ][ 51 ];

#define INF 1023456789

inline int AND(int msk, int id) {
    return ( msk&(1<<id) ) ;
}
inline int OR(int msk, int id ) {
    return ( msk|(1<<id) );
}
inline int XOR(int msk, int id) {
    return ( msk^(1<<id) );
}
inline void show(int msk) {
    for (int i=N-1;i>=0;--i) {
        cout<< ( AND(msk,i)==0 ? 0 : 1 ) ;
    }
    cout<<"\n";
}

int go(int msk, int last_point) {

    if ( msk==0 ) {
        return D[last_point][0];
    }

    int& best = dp[msk][last_point];
    if ( best!=-1 )return best;

    best = INF;
    for (int i=0;i<N;++i) {
        if ( AND(msk,i)!=0 ) {
            int v = D[last_point][ customer[i].f ]+
                    D[customer[i].f][customer[i].s]+
                    go( XOR(msk,i),customer[i].s );
            best = min( best, v );
        }
    }
    return best;
}

int TaxiManager :: schedule(vector <string> roads, vector <string> customers) {
    int retval=INF;
    RC = SZ( roads );
    fo(i,RC) {
        fo(j,RC) {
            if ( i==j ) {
                D[i][j]=0;
            }
            else {
                if ( roads[i][j]=='0' ) {
                    D[i][j]=INF;
                }
                else {
                    D[i][j]= ( roads[i][j]-'0' ) ;
                }
            }
        }
    }

    for (int k=0;k<RC;++k) {
        for (int i=0;i<RC;++i) {
            for (int j=0;j<RC;++j) {
                D[i][j] = min(D[i][j], D[i][k]+D[k][j]);
            }
        }
    }

    N = SZ( customers );
    fo(i,N) {
        int s,e;
        sscanf(customers[i].c_str(),"%d %d",&s,&e);
        customer[i] = mk(s,e);
    }

    SET(dp,-1);
    for ( int i=0,j=(1<<N); i<j; ++i ) {
        int taxi1msk = i;
        int taxi2msk = ( (~i)&(j-1) ) ;

        int time1 = go( taxi1msk,0 );
        int time2 = go( taxi2msk,0 );
//         show(taxi1msk);
//         show(taxi2msk);
//         printf("RESULT = %d %d\n",time1,time2);
        int max_time = max( time1, time2 );
        retval = min( retval, max_time );
    }
    return retval;
}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, vector <string> p0, vector <string> p1, bool hasAnswer, int p2) {
    cout << "Test " << testNum << ": [" << "{";
    for (int i = 0; int(p0.size()) > i; ++i) {
        if (i > 0) {
            cout << ",";
        }
        cout << "\"" << p0[i] << "\"";
    }
    cout << "}" << "," << "{";
    for (int i = 0; int(p1.size()) > i; ++i) {
        if (i > 0) {
            cout << ",";
        }
        cout << "\"" << p1[i] << "\"";
    }
    cout << "}";
    cout << "]" << endl;
    TaxiManager *obj;
    int answer;
    obj = new TaxiManager();
    clock_t startTime = clock();
    answer = obj->schedule(p0, p1);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << p2 << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << answer << endl;
    if (hasAnswer) {
        res = answer == p2;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
int main() {
    bool all_right;
    all_right = true;

    vector <string> p0;
    vector <string> p1;
    int p2;

    {
        // ----- test 0 -----
        string t0[] = {"020200","202020","020002","200020","020202","002020"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        string t1[] = {"5 3","2 4","1 5","3 2"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 16;
        all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 1 -----
        string t0[] = {"00020251090265906661","00763002550100090081","06003699000080062771","00000710460400035310","50000039119198350060","66060004050810046028","02333108565000200880","40212560000209205231","02601150098329905062","00210383709951005203","10111087340780827070","05065800003095040140","15604020082000100090","83430030070580600750","10588355007006001150","14400080790005400536","23400990400933060004","11053016300602000090","90040920084059282502","61300007077904050900"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        string t1[] = {"0 19","4 16","15 16","4 18","2 7","9 15","11 6","7 13","19 13","12 19","14 12","16 1"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 33;
        all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 2 -----
        string t0[] = {"095222800320504","107600288090501","760973530769345","963093337510830","338404069255826","291700050155264","002783031709004","404730701707712","068870030090995","320025180036103","468695042801904","233626561000105","070014432197086","887301000143802","230852749990330"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        string t1[] = {"3 6","0 4","2 7","9 7","13 9","1 6","7 13","14 2","8 7","10 1","11 13","7 12"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 28;
        all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 3 -----
        string t0[] = {"00401","50990","00062","08008","03000"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        string t1[] = {"2 4"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 14;
        all_right = KawigiEdit_RunTest(3, p0, p1, true, p2) && all_right;
        // ------------------
    }

    if (all_right) {
        cout << "You're a stud (at least on the example cases)!" << endl;
    } else {
        cout << "Some of the test cases had errors." << endl;
    }
    return 0;
}
// END KAWIGIEDIT TESTING


//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
// kate: indent-mode cstyle; space-indent on; indent-width 0; 

